#include "WorldMap.h"

void WorldMap::generateAlexander(){
	// << WORLD TERRAIN GENERATOR >>
	// << http://www.cartania.com/alexander/generation.html >>
	// Parameters:
	int numberOfPoints = 7; //number of starting points
	//int waterProb = 12000/_Map.size(); //water tile placement probability (0-100)
	int waterProb = 12000/(_tilesX*_tilesY); //water tile placement probability (0-100)
	int islandProb = 10; //island removal probability (0-100)

	/*float climateStr = (rand()%10+15)/10.0; //width of the one climate zone
	float climateTol = (rand()%20+10)/100.0; //tolerance in mixing zones
	float climateRotation = ((rand()%30)-10)/1000.0; //x axis influence at the climate zone*/

	// << Water and lands >>
	// 1. Set every cell in the world map to UNASSIGNED
	// (done in constructor)
	// 2. Empty the SparkList
	vector<int> xCord;
	vector<int> yCord;

	// 3. Randomly assign some points as land
	for(int i=0; i<numberOfPoints; i++){
		int x = rand()%_tilesX;
		int y = rand()%_tilesY;
		// 3.1 Check if there're no repeats
		bool repeat = false;
		for(unsigned int j=0; j<xCord.size(); j++){
			if(x == xCord[j] && y == yCord[j]){
				repeat = true;
				i--;
			}
		}
		if(repeat) continue;
		// 3.2 Mark them as LAND on the world map
		setLower(x,y,LAND);
		// 3.3 Add their map coordinates to the SparkList
		xCord.push_back(x);
		yCord.push_back(y);
	}

	// 4. Repeat until the SparkList is empty:
	while(!xCord.empty()){
		// 4.1 Pick a random element from the SparkList
		int rnd = rand()%xCord.size();
		int x = xCord[rnd];
		int y = yCord[rnd];
		int actualbiome = getLower(x,y);
			// 4.2 Check its eight adjacent neighbors on the world map in turn
			for(int i=-1; i<2; i++){
				for(int j=-1; j<2; j++){
					// 4.3 If UNASSIGNED:
					if(getLower(x+i,y+j) == UNDEF){
						// 4.3.1 Mark the neighbor as LAND or WATER on the world map
						if(actualbiome == LAND){
							bool beALand = rand()%100 > waterProb ? true : false;
							if(beALand) setLower(x+i,y+j,LAND);
							else setLower(x+i,y+j,WATER);
						}
						else{
							setLower(x+i,y+j,WATER);
						}
						// 4.3.2 Add the neighbor's map coordinates to the SparkList
						xCord.push_back(x+i);
						yCord.push_back(y+j);
					}
				}
			}

		// 4.4 Remove the old spark from the SparkList
		xCord.erase(xCord.begin()+rnd);
		yCord.erase(yCord.begin()+rnd);
	}

	// << Removing small water tiles >>
	for(int j=0; j<_tilesY; j++){
		for(int i=0; i<_tilesX; i++){
			if(getLower(i,j) == WATER){
				int landCount = 0;
				for(int k=-1; k<2; k++){
					for(int l=-1; l<2; l++){
						if(getLower(i+k,j+l) == LAND || getLower(i+k,j+l) == NONE) landCount++;
					}
				}
				if(landCount > 6 && rand()%100 > islandProb) setLower(i,j,LAND); // parameter: removal probability
			}
		}
	}

	/*// << Climate >>
	int climates = 7; // number of climate zones
	float northClimate = rand()%((int)((climates-climateStr)*100))/100.0;
	float aVal = climateStr/_tilesY;

	for(int j=0; j<_tilesY; j++){
		for(int i=0; i<_tilesX; i++){
			if(getLower(i,j) == LAND){
				float climateCon = aVal*j+northClimate+(i*climateRotation);
				float climateCheck = climateCon - (int)climateCon;
				int climate = (int)climateCon;
				if(climateCheck < climateTol && rand()%100 < ((climateTol-climateCheck)*100) ) climate--;
				else if(climateCheck > 1-climateTol && rand()%100 < ((climateCheck-(1-climateTol))*100)) climate++;
				EBiome tile;
				switch(climate){
				case 0: tile = POLAR; break;
				case 1: tile = TEMPERATE; break;
				case 2: tile = DRY; break;
				case 3: tile = TROPIC; break;
				case 4: tile = DRY; break;
				case 5: tile = TEMPERATE; break;
				default: tile = POLAR; break;
				}
				setbiome(i,j,tile);
			}
		}
	}*/
}